package com.hlx.service.admin.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hlx.entity.admin.Station;
import com.hlx.entity.admin.TrainNumber;
import com.hlx.entity.user.TrainNumberStation;
import com.hlx.mapper.admin.TrainNumberMapper;
import com.hlx.service.admin.TrainNumberService;
import com.hlx.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.core.Local;
import org.springframework.stereotype.Service;

import java.nio.channels.FileLock;
import java.sql.Time;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

@Slf4j
@Service
public class TrainNumberServiceImpl extends ServiceImpl<TrainNumberMapper, TrainNumber> implements TrainNumberService {
    @Override
    public List<TrainNumber> getTrainNumberByName(List<String> trainNum) {
        LambdaQueryWrapper<TrainNumber> queryWrapperTn = new LambdaQueryWrapper<>();
        queryWrapperTn.in(TrainNumber::getTrainNumberName,trainNum);
        List<TrainNumber> tn = this.list(queryWrapperTn);
        return tn;
    }

    @Override
    public List<TrainNumber> judgeTrain(List<String> trainNumber, Integer sequence, LocalDate date) {
        LambdaQueryWrapper<TrainNumber> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(TrainNumber::getTrainNumberName,trainNumber);
        List<TrainNumber> trainNumbers = this.list(queryWrapper);
        Iterator<TrainNumber> it = trainNumbers.iterator();
        while(it.hasNext()){
            TrainNumber tr = it.next();
            if(!tr.getSequence().equals(sequence) || !this.exitTrainData(tr.getTrainNumberName(),date)){
                it.remove();
            }
        }
        log.info("{}",trainNumbers);
        return trainNumbers;
    }

    @Override
    public Boolean exitTrainData(String trainNumber, LocalDate date) {
        LambdaQueryWrapper<TrainNumber> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainNumber::getTrainNumberName,trainNumber);
        if(date.equals(this.getOne(queryWrapper).getStartDate())
                || date.equals(this.getOne(queryWrapper).getEndDate())
                || date.isBefore(this.getOne(queryWrapper).getEndDate()) && date.isAfter(this.getOne(queryWrapper).getStartDate())){
            return true;
        }
        return false;
    }

    @Override
    public TrainNumber addTrainNumber(TrainNumber trainNumber, Integer distance, Integer velocity,Integer stopTime) {
//        Float totalF = (float)distance / velocity;
//        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
//        String formattedResult = decimalFormat.format(totalF);
//        Float minuteF = Float.parseFloat(formattedResult) * 60;
//        Integer minuteI = Math.round(minuteF);
//        Integer min = minuteI % 60;
//        Integer hour = minuteI / 60;
//        LocalTime startTime = trainNumber.getStartTime();
//        LocalTime endTime = LocalTime.of(startTime.getHour()+hour,startTime.getMinute()+min,0);
        TimeUtils timeUtils = new TimeUtils();
//        trainNumber.setEndTime(trainNumber.getStartTime());
//        trainNumber.setStartDate(trainNumber.getStartDate().plusDays(1));
        trainNumber.setEndTime(timeUtils.calculateTime(distance,velocity,trainNumber.getStartTime()));
        LocalDate startDate = trainNumber.getStartDate();
        LocalDate endDate = startDate;
        endDate = endDate.plusDays(15);
        trainNumber.setEndDate(endDate);
        this.save(trainNumber);
        return this.getById(trainNumber.getId());
    }


}
